/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.internal.panel;

import java.awt.Component;

import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.gui.view.internal.FormDocumentInternalFrame;
import com.genia.toolbox.form.editor.model.form.impl.ContainerFormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormType;

/**
 * The vertical form container view panel.
 */
@SuppressWarnings("serial")
public class LabeledFormViewPanel
    extends AbstractContainerFormViewPanel
{

  /**
   * Constructor.
   * 
   * @param gui
   *          The gui.
   * @param internalFrame
   *          The internal frame.
   */
  public LabeledFormViewPanel(FormEditorGUI gui, FormDocumentInternalFrame internalFrame)
  {
    super(gui, internalFrame);
  }



  /**
   * Create the element type.
   * 
   * @return the element type.
   */
  @Override
  public FormType getElementType()
  {
    return FormType.LABELED;
  }



  /**
   * Set the displayed element.
   * 
   * @param model
   *          the displayed element model.
   */
  @Override
  public void setElement(FormModel model)
  {
    super.setElement(model);
    ContainerFormModel containerElementModel = (ContainerFormModel) this.getElement();
    String key = containerElementModel.getLabelKey();
    if (key == null) {
      key = "";
    }
    this.getLabelType().setText(model.getType().name() + " : " + key);
  }



  /**
   * Display the popup.
   * 
   * @param component
   *          The component.
   * @param x
   *          The x position.
   * @param y
   *          The y position.
   */
  @Override
  public void displayPopup(Component component, int x, int y)
  {
    super.displayPopup(component, x, y);
    ContainerFormModel containerElementModel = (ContainerFormModel) this.getElement();
    int nbDisplayedSubElements = containerElementModel.getNbSubForms();
    System.out.println(containerElementModel.getType().name() + " nbDisplayedSubElements : " + nbDisplayedSubElements + " / " + containerElementModel.getMaxNbSubForms());
    if (nbDisplayedSubElements >= containerElementModel.getMaxNbSubForms()) {
      this.getMenuAddForm().setEnabled(false);
    }
    else {
      this.getMenuAddForm().setEnabled(true);
    }
    this.getMenuAdd().setEnabled(false);
  }

}
